#import "dfhgwhruebvhrheymournrural.h"
#import "dfhgwhruebvhrheysternideology.h"
#import "dfhgwhruebvhrheyoutwarddelegate.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheythrivealternate.h"
#import "dfhgwhruebvhrheycandidateroutineViewController.h"
#import "dfhgwhruebvhrheyquantifysignifyViewController.h"
#import <FirebaseAnalytics/FirebaseAnalytics.h>
@interface dfhgwhruebvhrheyquantifysignifyViewController ()
@property(nonatomic, strong) dfhgwhruebvhrheymournrural *timer;
@end
@implementation dfhgwhruebvhrheyquantifysignifyViewController {
    int cut_down;
    UIView *bgView;
    UIImageView *icon;
    UIScrollView *scrollView;
    UIView *containerView;
    UILabel *textLabel;
    UIButton *sendBtn;
    UILabel *codeText;
    dfhgwhruebvhrheythrivealternate *code;
    UILabel *passwdText;
    dfhgwhruebvhrheythrivealternate *passwd;
    UIButton *registerBtn;
}
- (dfhgwhruebvhrheymournrural *)timer {
    if (_timer == nil) {
        _timer = [[dfhgwhruebvhrheymournrural alloc] init];
    }
    return _timer;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputChange:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUI];
    [self sendRegSms];
}
- (void)startTimer {
    sendBtn.enabled = NO;
    sendBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
    [self.timer startWithAction:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateCutTime];
        });
    }];
}
- (void)updateCutTime {
    if (cut_down <= 1) {
        cut_down = dhgfywegwygeydwleakconceal;
        [_timer cancel];
        _timer = nil;
        sendBtn.enabled = YES;
        [sendBtn setTitle:@"Send" forState:UIControlStateNormal];
        sendBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    }else {
        cut_down -= 1;
        [sendBtn setTitle:[NSString stringWithFormat:@"%02ds", cut_down] forState:UIControlStateNormal];
    }
}
- (void)sendRegSms {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.mobile;
    params[@"pgid"] = @"1";
    [dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
    __weak typeof(self) weakSelf = self;
    [[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:registerCodeApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
        if (model.code == 200) {
            [strongSelf startTimer];
        }else{
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
    }];
}
- (void)registerAccount {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"code"] = code.content;
    params[@"mobile"] = self.mobile;
    params[@"password"] = passwd.content;
    params[@"pgid"] = @"1";
    params[@"type"] = @"2";
    params[@"device_id"] = dfhgwhruebvhrheyoptionsaving.uuid.dhgfywegwygeydwscholarshiptexture;
    params[@"version"] = dfhgwhruebvhrheyoptionsaving.appVersionString;
    [dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
    __weak typeof(self) weakSelf = self;
    [[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:registerApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
        if (model.code == 200) {
            [FIRAnalytics logEventWithName:@"iOS_sign_up_zhuce" parameters:nil];
            [strongSelf autoLogin];
        }else {
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
    }];
}
- (void)autoLogin {
    NSString *riskJson = [dfhgwhruebvhrheyoptionsaving getRiskJsonData];
    NSMutableDictionary *params = [@{} mutableCopy];
    params[@"device_id"] = [[dfhgwhruebvhrheyoptionsaving uuid] dhgfywegwygeydwscholarshiptexture];
    params[@"mobile"] = self.mobile;
    params[@"pgid"] = @"1";
    params[@"password"] = passwd.content;
    params[@"type"] =  @"2";
    params[@"version"] = [dfhgwhruebvhrheyoptionsaving appVersionString];
    params[@"rdata"] = riskJson;
    __weak typeof(self) weakSelf = self;
    [[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:loginApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        dfhgwhruebvhrheyoutwarddelegate *model = [dfhgwhruebvhrheyoutwarddelegate yy_modelWithJSON:json];
        if (model.code == 200) {
            [dfhgwhruebvhrheyoptionsaving saveUserInfo:json completion:^{
                [dfhgwhruebvhrheyoptionsaving uploadRiskDataByCatchId:@"4" andApplyId:nil withDetailId:nil];
                [strongSelf goHome];
                [strongSelf dismissViewControllerAnimated:YES completion:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:dhgfywegwygeydwembarrassurgent object:nil];
            }];
        }else {
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
    }];
}
- (void)goHome {
    dfhgwhruebvhrheycandidateroutineViewController *tabBar = (dfhgwhruebvhrheycandidateroutineViewController *)[dfhgwhruebvhrheyoptionsaving.keyWindow rootViewController];
    tabBar.selectedIndex = 0;
}
- (void)loadUI {
    cut_down = dhgfywegwygeydwleakconceal;
    [self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
    bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 20;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    icon = [[UIImageView alloc] init];
    icon.image = [UIImage imageNamed:@"ppp_login_logo"];
    [bgView addSubview:icon];
    scrollView = [[UIScrollView alloc] init];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:scrollView];
    containerView = [[UIView alloc] init];
    containerView.backgroundColor = white_color.dhgfywegwygeydwlearneddesignate;
    [scrollView addSubview:containerView];
    textLabel = [[UILabel alloc] init];
    textLabel.font = [UIFont regular:15];
    textLabel.numberOfLines = 0;
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.textColor = text_color4.dhgfywegwygeydwlearneddesignate;
    [containerView addSubview:textLabel];
    if (![self.mobile hasPrefix:@"0"]) {
        self.mobile = [NSString stringWithFormat:@"0%@",self.mobile];
    }
    NSString *mobile = [self.mobile stringByReplacingCharactersInRange:NSMakeRange(4, 4) withString:@"****"];
    textLabel.text = [NSString stringWithFormat:@"Your verification code is sent via SMS to: %@", mobile];
    sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sendBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    sendBtn.frame = CGRectMake(0, 0, 70, 40);
    sendBtn.tag = 100;
    sendBtn.layer.cornerRadius = 8;
    [sendBtn setTitle:@"Send" forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    codeText = [[UILabel alloc] init];
    codeText.text = @"Verification code";
    codeText.font = [UIFont regular:16];
    codeText.textColor = text_color1.dhgfywegwygeydwlearneddesignate;
    [containerView addSubview:codeText];
    code = [[dfhgwhruebvhrheythrivealternate alloc] init];
    [containerView addSubview:code];
    code.tag = 100;
    code.rightBtn = sendBtn;
    code.placeholder = @"A 4-digit OTP";
    [containerView addSubview:code];
    passwdText = [[UILabel alloc] init];
    passwdText.text = @"Password";
    passwdText.font = [UIFont regular:16];
    passwdText.textColor = text_color1.dhgfywegwygeydwlearneddesignate;
    [containerView addSubview:passwdText];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 101;
    rightBtn.frame = CGRectMake(0, 0, 24, 24);
    [rightBtn setImage:[UIImage imageNamed:@"dhgfywegwygeydwreceiptaffirm"] forState:UIControlStateSelected];
    [rightBtn setImage:[UIImage imageNamed:@"dhgfywegwygeydwaffirmsalesman"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    passwd = [[dfhgwhruebvhrheythrivealternate alloc] init];
    [containerView addSubview:passwd];
    passwd.rightBtn = rightBtn;
    passwd.tag = 101;
    passwd.secureTextEntry = YES;
    passwd.placeholder = @"Please enter a 6-digit password";
    registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.tag = 102;
    registerBtn.enabled = NO;
    registerBtn.layer.cornerRadius = 10;
    registerBtn.titleLabel.font = [UIFont medium:16];
    [registerBtn setTitle:@"Sign up" forState:UIControlStateNormal];
    registerBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
    [registerBtn setTitleColor:white_color.dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:registerBtn];
}
- (void)inputChange:(NSNotification *)not {
    UITextField *input = [not object];
    NSString *text = input.text;
    if (input.superview.tag == 100) {
        if (text.length >= 4) {
            code.content = [text substringToIndex:4];
            if (passwd.content.length == 6) {
                registerBtn.enabled = YES;
                registerBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
            }else {
                registerBtn.enabled = NO;
                registerBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
            }
        }else {
            code.content = input.text;
            registerBtn.enabled = NO;
            registerBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
        }
    }else {
        if (text.length >= 6) {
            passwd.content = [text substringToIndex:6];
            if (code.content.length == 4) {
                registerBtn.enabled = YES;
                registerBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
            }else {
                registerBtn.enabled = NO;
                registerBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
            }
        }else {
            passwd.content = input.text;
            registerBtn.enabled = NO;
            registerBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
        }
    }
}
- (void)btnActions:(UIButton *)sender {
    switch (sender.tag) {
        case 100: {
            [self sendRegSms];
        }
            break;
        case 101: {
            sender.selected = !sender.selected;
            passwd.secureTextEntry = !sender.isSelected;
        }
            break;
        case 102: {
            if (code.content.length != 4) {
                return [self.view show:@"please enter a 4-digit code"];
            }
            if (passwd.content.length != 6) {
                return [self.view show:@"Enter 6-digit password"];
            }
            [self registerAccount];
        }
            break;
        default:
            break;
    }
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(48);
        make.left.bottom.right.equalTo(self.view);
    }];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView);
        make.top.equalTo(bgView).offset(-40);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(icon.mas_bottom).offset(16);
        make.left.bottom.right.equalTo(bgView);
    }];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(containerView).offset(-60);
        make.left.equalTo(containerView).offset(12);
        make.top.equalTo(containerView).offset(4);
        make.height.mas_equalTo(46);
    }];
    [codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(24);
        make.left.centerX.equalTo(textLabel);
        make.height.mas_equalTo(24);
    }];
    [code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeText.mas_bottom).offset(12);
        make.centerX.equalTo(containerView);
        make.left.equalTo(codeText);
        make.height.mas_equalTo(56);
    }];
    [passwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(code.mas_bottom).offset(24);
        make.left.centerX.equalTo(code);
        make.height.mas_equalTo(24);
    }];
    [passwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwdText.mas_bottom).offset(12);
        make.left.centerX.equalTo(passwdText);
        make.height.mas_equalTo(56);
    }];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwd.mas_bottom).offset(40);
        make.left.centerX.equalTo(passwd);
        make.height.mas_equalTo(56);
        make.bottom.equalTo(containerView);
    }];
}
@end
